# author: dcc

import requests


def send_message(total_num_fakao, bug_num_fakao, other_num_fakao, total_num_fashuo, bug_num_fashuo, other_num_fashuo):
    url_send = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=def43ad6-ea81-4a07-a73e-700c90068fd9"
    header_send = {"Content-Type": "application/json"}
    if total_num_fakao > 0 and total_num_fashuo <= 0:
        send_str = "法考待处理问题数：{}，Bug反馈：{}，其他：{}".format(total_num_fakao, bug_num_fakao, other_num_fakao)
    elif total_num_fakao <= 0 and total_num_fashuo > 0:
        send_str = "法硕待处理问题数：{}，Bug反馈：{}，其他：{}".format(total_num_fashuo, bug_num_fashuo, other_num_fashuo)
    else:
        send_str = "法考待处理问题数：{}，Bug反馈：{}，其他：{}\n法硕待处理问题数：{}，Bug反馈：{}，其他：{}".format(total_num_fakao,
                                                                                   bug_num_fakao, other_num_fakao,
                                                                                   total_num_fashuo, bug_num_fashuo,
                                                                                   other_num_fashuo)
    body_send = {
        "msgtype": "text",
        "text": {"content": send_str}
    }
    res = requests.post(url=url_send, json=body_send, headers=header_send)
    code = res.json()["errcode"]
    assert code == 0, res.text


# 法考：config_id（1-BUG反馈，19-其他）
def find_questions_fakao(config_id):
    url_find = "https://api.juexiaotime.com/faqapi/helpFeedback/selectFeedbackListByParam"
    header_admin = {"Content-Type": "application/json;charset=utf-8", "source": "admin", "mode": "debug"}
    body_find = {"configId": str(config_id),
                 "status": 1,
                 "pageRow": 20,
                 "param": "",
                 "mockType": 1,
                 "operateId": 782,
                 "source": "admin",
                 "env": "product"
                 }
    res = requests.post(url=url_find, json=body_find, headers=header_admin)
    code = res.json()["code"]
    assert code == 0, res.text
    total_count = res.json()["data"]["totalCount"]
    return total_count


# 法硕：config_id（1-BUG反馈，19-其他）
def find_questions_fashuo(config_id):
    url_find = "https://api.juexiaotime.com/faqapi/helpFeedback/selectFeedbackListByParam"
    header_admin = {"Content-Type": "application/json;charset=utf-8", "source": "admin", "mode": "debug"}
    body_find = {"configId": str(config_id),
                 "status": 1,
                 "pageRow": 20,
                 "param": "",
                 "mockType": 2,
                 "operateId": 782,
                 "source": "admin",
                 "env": "product"
                 }
    res = requests.post(url=url_find, json=body_find, headers=header_admin)
    code = res.json()["code"]
    assert code == 0, res.text
    total_count = res.json()["data"]["totalCount"]
    return total_count


if __name__ == "__main__":
    bug_nums_fakao = find_questions_fakao(1)
    other_nums_fakao = find_questions_fakao(19)
    total_nums_fakao = bug_nums_fakao + other_nums_fakao
    bug_nums_fashuo = find_questions_fashuo(1)
    other_nums_fashuo = find_questions_fashuo(19)
    total_nums_fashuo = bug_nums_fashuo + other_nums_fashuo
    all_nums = total_nums_fakao + total_nums_fashuo
    if all_nums > 0:
        send_message(total_num_fakao=total_nums_fakao, bug_num_fakao=bug_nums_fakao, other_num_fakao=other_nums_fakao,
                     total_num_fashuo=total_nums_fashuo, bug_num_fashuo=bug_nums_fashuo,
                     other_num_fashuo=other_nums_fashuo)
